
from whiff import gateway
import diskUsage
import os
import urllib

class du:
    def __init__(self, topDirectory):
        self.directory = topDirectory
        (c,t,s) = diskUsage.diskUsageDictionary(topDirectory)
        self.dictionary = c
    def localize(self, *args):
        return self
    def get(self, pathlist):
        if list(pathlist)==["root"]:
            return self.directory
        assert len(pathlist)==2, 'expect ["files", path] or ["dirs", path] '+repr(pathlist)
        (directive, path) = pathlist
        path = path.strip()
        (total, subdirs) = self.dictionary[path]
        if directive=="files":
            return str(subdirs["[files]"])
        elif directive=="dirs":
            its = []
            for (n,v) in subdirs.items():
                if n!="[files]":
                    fn = os.path.split(n)[-1]
                    n = urllib.quote(n)
                    v1 = "%5.2eM" % (v/1000000.0)
                    its.append( (n, str(v), fn, v1 ))
            its.sort()
            return its
        else:
            assert False, "expect directive 'files' or 'dirs' only "+repr(pathlist)

def test():
    d = du("amCharts")
    root = d.get(["root"])
    print "testing"
    print "root", root
    print "file usage", d.get(["files", root])
    print "dirs", d.get(["dirs", root])

if __name__=="__main__":
    test()
